/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.core.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.core.dao.StateDAO;
import com.core.database.Manager;
import com.core.entities.State;
import com.core.exception.DAOException;

/**
 * 
 * @author himanasser
 */
public class StateDAOImpl implements StateDAO {
		State state = new State();
		ArrayList<State> stateList = new ArrayList<State>();

	Connection connection = null;
	private static StateDAOImpl instance = null;
	public static StateDAOImpl getInstance(){
		if(instance == null){
			instance = new StateDAOImpl();
		}
		return instance;
	}

	@Override
	public State findByName(String name) throws DAOException {
		try {
			connection = Manager.getConnection();
			String sql = "SELECT * FROM state WHERE name = '" + name + "'";
			PreparedStatement ps = connection.prepareStatement(sql);
			ResultSet rs = ps.executeQuery();
			int count = 0;
			while (rs.next()) {
				count++;
				if (count == 1) {
					state.setId(rs.getInt(1));
					state.setName(rs.getString(2));
					state.setCountry_id(rs.getInt(3));

				}
			}

		} catch (SQLException s) {
			s.printStackTrace();
		}
		return state;
	}

	@Override
	public List<State> findByCountry_id(int country_id) throws DAOException {
		try {
			connection = Manager.getConnection();
			String sql = "SELECT * FROM state WHERE country_id = '"
					+ country_id + "'";
			PreparedStatement ps = connection.prepareStatement(sql);
			ResultSet rs = ps.executeQuery();
			while (rs.next()) {
				State state = new State();
				state.setId(rs.getInt(1));
				state.setName(rs.getString(2));
				state.setCountry_id(rs.getInt(3));
				stateList.add(state);

			}
		} catch (SQLException s) {
			s.printStackTrace();
		}
		return stateList;
	}

	@Override
	public void insert(State o) throws DAOException {
		Connection connection = null;
		try {
			connection = Manager.getConnection();
			String sql = " INSERT INTO  state (name, country_id) VALUE (?, ?)";
			PreparedStatement ps = connection.prepareStatement(sql);
			ps.setString(1, o.getName());
			ps.setInt(2, o.getCountry_id());

			int result = ps.executeUpdate();
			if (result != 1) {
				throw new SQLException("no row inserted!");
			}
		} catch (Exception ex) {
			ex.printStackTrace();
			throw new RuntimeException("Cannot add new State", ex);
		} finally {
			Manager.closeConnection(connection);
		}
	}

	@Override
	public State findById(int id) throws DAOException {
		try {
			connection = Manager.getConnection();
			String sql = "SELECT * FROM state WHERE id = '" + id + "'";
			PreparedStatement ps = connection.prepareStatement(sql);
			ResultSet rs = ps.executeQuery();
			int count = 0;
			while (rs.next()) {
				count++;
				if (count == 1) {
					state.setId(rs.getInt(1));
					state.setName(rs.getString(2));
					state.setCountry_id(rs.getInt(3));

				}
			}

		} catch (SQLException s) {
			s.printStackTrace();
		}
		return state;
	}

	@Override
	public List<State> findAll() throws DAOException {
		try {
			connection = Manager.getConnection();
			String sql = " SELECT * FROM state ";
			PreparedStatement ps = connection.prepareStatement(sql);
			ResultSet rs = ps.executeQuery();
			while (rs.next()) {
				state.setId(rs.getInt(1));
				state.setName(rs.getString(2));
				state.setCountry_id(rs.getInt(3));

				stateList.add(state);

			}
		} catch (SQLException s) {
			s.printStackTrace();
		}
		return stateList;
	}

	@Override
	public void delete(int id) throws DAOException {
		try {
			connection = Manager.getConnection();
			String sql = " DELETE FROM state WHERE id = '" + id + "'";
			PreparedStatement ps = connection.prepareStatement(sql);
			int result = ps.executeUpdate();
			if (result != 1) {
				ps.setString(1, state.getName());
				ps.setInt(2, state.getCountry_id());
			}

		} catch (SQLException s) {
			s.printStackTrace();

		}
	}

	@Override
	public void update(State o) throws DAOException {
		try {
			connection = Manager.getConnection();
			String sql = "UPDATE state SET name = '" + o.getName()
					+ " ', country_id = '" + o.getCountry_id()
					+ "' WHERE id = " + o.getId();
			PreparedStatement ps = connection.prepareStatement(sql);
			int result = ps.executeUpdate(sql);

			if (result != 1) {
				ps.setString(1, o.getName());
				ps.setInt(2, o.getCountry_id());
			}

		} catch (SQLException s) {
			s.printStackTrace();
		}
	}
}
